//
//  EmojiView.m
//  ECBBusinessCamp
//
//  Created by rimi on 16/6/23.
//  Copyright © 2016年 邓毕华. All rights reserved.
//

#import "EmojiView.h"

#define KEYBORD_WIDTH 840 // 表情键盘宽度
#define KEYBORD_HEIGHT 240 // 表情键盘高度
#define EMOJI_WIDTH 60 // 表情宽度
#define PAGE_NUMBER 55 // 一页表情的数量

@interface EmojiView ()<UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView * emojiScrollView; // 表情滚动视图
@property (nonatomic, strong) UIPageControl * emojiPageControl; // 表情分页控件
@property (nonatomic, strong) NSArray * emojiArray; // 表情数组

@end

@implementation EmojiView

#pragma mark 🌷🌷🌷life cycle🌷🌷🌷
- (instancetype)initWithFrame:(CGRect)frame selecEmojiIndexBlock:(SelectEmojiIndexBlock)selecEmojiIndexBlock
{
    self = [super initWithFrame:frame];
    if (self) {
        // 表情按钮回调
        _selecEmojiIndexBlock = selecEmojiIndexBlock;
        
        // 添加视图
        [self addSubview:self.emojiScrollView];
        [self addSubview:self.emojiPageControl];
        self.backgroundColor = RGB_COLOR(239, 239, 239, 1);
        
        // 自动布局
        [_emojiScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.offset(KEYBORD_WIDTH);
            make.height.equalTo(self).multipliedBy(0.85);
            make.centerX.equalTo(self);
            make.top.offset(SCREEN_HEIGHT * 0.02);
        }];
        [_emojiPageControl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.sizeOffset(CGSizeMake(SCREEN_WIDTH * 0.5, SCREEN_HEIGHT * 0.1));
            make.centerX.equalTo(self);
            make.bottom.offset(0);
        }];
    }
    return self;
}

#pragma mark 🌷🌷🌷UIScrollViewDelegate🌷🌷🌷
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    _emojiPageControl.currentPage = scrollView.contentOffset.x / KEYBORD_WIDTH;
}

#pragma mark 🌷🌷🌷event responds🌷🌷🌷
- (void)respondsToSelectEmojiBtn:(UIButton *)sender {
    _selecEmojiIndexBlock(sender.tag - 11);
}

#pragma mark 🌷🌷🌷getters and setters🌷🌷🌷
- (UIScrollView *)emojiScrollView {
    if (!_emojiScrollView) {
        _emojiScrollView = [[UIScrollView alloc] init];
        
        // 设置代理
        _emojiScrollView.delegate = self;
        
        _emojiScrollView.contentSize = CGSizeMake(KEYBORD_WIDTH * ceilf(self.emojiArray.count / (CGFloat)PAGE_NUMBER), KEYBORD_HEIGHT);
        
        // 设置分页
        _emojiScrollView.pagingEnabled = YES;
        
        _emojiScrollView.showsHorizontalScrollIndicator = NO;
        
        for (NSInteger i = 0; i < self.emojiArray.count; i++) {
            // 添加表情按钮
            UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = CGRectMake(i % PAGE_NUMBER % 14 * EMOJI_WIDTH + i / PAGE_NUMBER * KEYBORD_WIDTH, i % PAGE_NUMBER / 14 * EMOJI_WIDTH, EMOJI_WIDTH, EMOJI_WIDTH);
            button.titleLabel.font = [UIFont systemFontOfSize:40];
            button.tag = i + 11;
            [button addTarget:self action:@selector(respondsToSelectEmojiBtn:) forControlEvents:UIControlEventTouchUpInside];
            [button setTitle:self.emojiArray[i] forState:UIControlStateNormal];
            [_emojiScrollView addSubview:button];
            
            // 删除按钮
            if (i / (CGFloat)PAGE_NUMBER == i / PAGE_NUMBER) {
                UIButton * deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
                deleteBtn.frame = CGRectMake(i / PAGE_NUMBER * KEYBORD_WIDTH + 13 * EMOJI_WIDTH + 5, 3 * EMOJI_WIDTH + 12, 50, 35);
                deleteBtn.tag = 10;
                [deleteBtn addTarget:self action:@selector(respondsToSelectEmojiBtn:) forControlEvents:UIControlEventTouchUpInside];
                [deleteBtn setImage:[UIImage imageNamed:@"delete"] forState:UIControlStateNormal];
                [_emojiScrollView addSubview:deleteBtn];
            }
        }
    }
    return _emojiScrollView;
}
- (NSArray *)emojiArray {
    if (!_emojiArray) {
        _emojiArray = [Emoji allEmoji];
    }
    return _emojiArray;
}
- (UIPageControl *)emojiPageControl {
    if (!_emojiPageControl) {
        _emojiPageControl = [[UIPageControl alloc] init];
        _emojiPageControl.numberOfPages = ceilf(_emojiArray.count / PAGE_NUMBER);
        _emojiPageControl.pageIndicatorTintColor = RGB_COLOR(195, 195, 195, 1);
        _emojiPageControl.currentPageIndicatorTintColor = RGB_COLOR(144, 144, 144, 1);
    }
    return _emojiPageControl;
}

@end
